<template>
  <div class="wrap">
    <div class="searchBox">
      <el-input
        prefix-icon="el-icon-search"
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width:30%"
        clearable
      ></el-input>
    </div>
    <!-- 列表 -->
    <el-table
      ref="table"
      :data="tableData.slice((currpage - 1) * pagesize, currpage * pagesize)"
      border
      style="width: 100%"
      stripe
      @selection-change="handleSelectionChange"
      @sort-change="changeSort"
      :row-key="getRowKeys"
    >
      <!-- <el-table-column
        type="selection"
        width="40"
        align="center"
        :reserve-selection="true"
        :key="Math.random()"
      ></el-table-column> -->
      <el-table-column
        prop="org_id"
        label="标识ID"
        min-width="230"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.org_id
        }}</template>
      </el-table-column>
      <el-table-column
        prop="gfather_name"
        label="上上级分组名称 "
        min-width="140"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.gfather_name
        }}</template>
      </el-table-column>
      <el-table-column
        prop="father_name"
        label="上级分组名称"
        min-width="140"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.father_name
        }}</template>
      </el-table-column>
      <el-table-column
        prop="org_name"
        label="分组名称"
        min-width="120"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.org_name
        }}</template>
      </el-table-column>
      <el-table-column
        prop="created_by"
        label="创建者"
        min-width="120"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.created_by
        }}</template>
      </el-table-column>
      <el-table-column
        prop="created_time"
        label="创建时间"
        min-width="160"
        align="center"
        :key="Math.random()"
      >
        <template slot-scope="scope">{{
          scope.row.created_time
        }}</template>
      </el-table-column>
      <el-table-column
        class="play-box"
        label="操作"
        min-width="240"
        fixed="right"
        align="center"
        :key="Math.random()"
        v-if="permissionData.hideActionArr.length < 3"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="success"
            @click="modifyFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['org/update'],
              index: scope.$index,
              key: 'hideAction',
              id: 'update',
            }"
            >修改</el-button
          >
          <el-button
            size="small"
            type="primary"
            @click="districtFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['org/device/add'],
              index: scope.$index,
              key: 'hideAction',
              id: 'shanchu',
            }"
            >分配设备</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="delRow(scope.$index, scope.row)"
            v-permissions="{
              url: ['org/delete'],
              index: scope.$index,
              key: 'hideAction',
              id: 'shouquan',
            }"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      right
      background
      layout="total,prev, pager, next, sizes,jumper"
      :page-sizes="pageSizesArr"
      :page-size="pagesize"
      :total="tableData.length"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <!-- 点击修改，弹出框 -->
    <OrgModify
      :dialogVisible_modify="dialogVisible_modify"
      v-if="org_id !== ''"
      :org_id="org_id"
      :gfather_name="gfather_name"
      :father_name="father_name"
      :org_name="org_name"
      @refreshTableListModify="refreshTableListModify"
    />
    <!-- 点击分配设备，弹出框 -->
    <OrgDistrict
      v-if="dialogVisible.show"
      :dialogVisible="dialogVisible"
      :org_id_check="org_id"
      :org_name="org_name"
    />
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import OrgModify from "./OrgModify";
 import OrgDistrict from "./OrgDistrict"; //旧的 左右left right
//import OrgDistrict from "./OrgDistributeDevice.vue";   //新做的tree

export default {
  name: "",
  props: {},
  components: { OrgModify, OrgDistrict },
  data() {
    return {
      currpage: 1, //分页
      pagesize: globalPageSizes[0], //初始显示，一页几个
      pageSizesArr: globalPageSizes, //globalPageSizes在public--config中全局配置，每页显示个数数组
      searchTableInfo: "", //搜索条件
      getSearchInfo: [], //搜索后，过滤后的tableData数据
      tableChecked: [], // 多选
      batchDelectId: [], //批量删除id
      checkAll: false, // 是否全选状态
      //修改弹出框
      dialogVisible_modify: {
        show: false,
      },
      //分配设备弹出框
      dialogVisible: {
        show: false,
      },
      org_id: "", //打开弹窗，当前行org_id
      org_name: "", //打开弹窗，当前行org_id
      permissionData: {
        hideActionArr: [], // 操作按钮的隐藏
      },
      gfather_name:"",
      father_name:"",
    };
  },
  computed: {
    // 根据计算属性模糊搜索
    tableData() {
      const searchTableInfo = this.searchTableInfo;
      //console.log(searchTableInfo);
      if (searchTableInfo) {
        return this.getSearchInfo.filter((item) => {
          return Object.keys(item).some((key) => {
            return (
              String(item[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            );
          });
        });
      }
      return this.getSearchInfo;
    },
    //刷新table
    submitData() {
      return this.$store.state.submitData;
    },
  },
  watch: {
    //刷新table
    submitData(newVal, oldVal) {
      if (newVal != oldVal) {
        this.getList();
        this.currpage = 1;
      }
    },
  },
  created() {},
  mounted() {
    this.getList();
  },
  methods: {
    // 获取row的key值
    getRowKeys(row) {
      return row.org_id;
    },
    //点击 第几页
    handleCurrentChange(cpage) {
      this.currpage = cpage;
    },
    //每页有几条
    handleSizeChange(psize) {
      this.pagesize = psize;
    },
    //操作多选
    handleSelectionChange(val) {
      var that = this;
      this.batchDelectId = [];
      that.tableChecked = val;
      if (val.length > 0) {
        for (let i = 0; i < val.length; i++) {
          this.batchDelectId.push(val[i]["role_id"]);
        }
      }
    },
    //排序
    changeSort(val) {
      // console.log(val);
    },
    //获取初始列表数据
    getList() {
      postData("/adminweb/org/list", {})
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            let orgs = res.datas.orgs;
            this.getSearchInfo = orgs;
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //删除单行
    delRow(index, rows) {
      //让接口改成数组
      // var subArr = [];
      // subArr.push(rows.org_id);
      var that = this;
      that
        .$confirm("此操作将删除:" + rows.org_name + ", 是否继续?", "删除", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          postData("/adminweb/org/delete", {
            org_id: rows.org_id,
          })
            .then((res) => {
              that.logining = false;
              // console.log(res);
              if (res.code == "0") {
                that.$message.success("删除成功！")
                that.getList();
              } else {
                that.$message.error(res.code + ":" + res.msg);
              }
            })
            .catch((res) => {
              that.$message.error(res);
            });
        })
        .catch((res) => {
          that.$message.success("已取消");
        });
    },
    //点击修改，弹窗
    modifyFn(index, row) {
      this.dialogVisible_modify = { show: true };
      this.org_id = row.org_id;
      this.gfather_name = row.gfather_name;
      this.father_name = row.father_name;
      this.org_name = row.org_name;
    },
    //点击分配设备，弹窗
    districtFn(index, row) {
      this.dialogVisible = { show: true };
      this.org_id = row.org_id;
      this.org_name = row.org_name;
    },
    refreshTableList() {
      this.getList();
    },
    refreshTableListModify() {
      this.dialogVisible_modify = { show: false };
      this.getList();
    },
  },
};
</script>
<style scoped>
/* 更改table,表头背景 */
.wrap >>> .el-table--border th,
.el-table__fixed-right-patch {
  /* background: #c0dee2; */
  background: #f0f3f4;
}
.wrap {
  margin-top: 20px;
  padding: 15px;
  overflow: hidden;
}
.searchBox {
  margin-bottom: 20px;
}
.wrap >>> .el-pagination {
  float: right;
  margin-top: 30px;
}

.batch-btn-con {
  margin-top: 70px;
}
.wrap >>> .tips {
  margin-top: 10px;
  display: block;
  font-size: 15px;
}
</style>
